const goodsType = require("../models/goodsType")
const goods = require("../models/goods")
const { body, check, checkSchema } = require("express-validator")
const Mock = require("mockjs")
const Random = Mock.Random

// 模拟数据接口
// exports.setGoods = [
// 	(req, res, next) => {
// 		const Goods = new goods({
// 			cover: Random.image("200x100"),
// 			name: Random.title(),
// 			description: Random.cparagraph(),
// 			createdTime: Date.now(),
// 			updatedTime: Date.now(),
// 			price: Random.integer(20, 40),
// 			goodsTypeId: "63e71c09d0f7c47e678f6fb5"
// 		})
// 		Goods.save((err) => {
// 			res.status(201).json({ status: 0, message: "添加成功" })
// 		})
// 	}
// ]
// 请求食品类型
exports.getGoodsType = [
	(req, res, next) => {
		goodsType.find().exec((err, find) => {
			res.json({
				status: 0,
				data: find
			})
		})
	}
]

// 请求对应类型的食品列表
exports.getGoods = [
	checkSchema({
		goodsTypeId: {
			in: ["params", "query"],
			trim: true,
			notEmpty: true,
			errorMessage: "食品类型id传递错误!"
		}
	}),
	(req, res, next) => {
		goods.find({ goodsTypeId: req.params.goodsTypeId }).exec((err, find) => {
			res.json({
				status: 0,
				total : find.length,
				data: find
			})
		})
	}
]
